package com.transcoachapp;

import java.util.ArrayList;

import com.transcoachapp.adapter.AutoCompleteAdapter;
import com.transcoachapp.data.Station;
import com.transcoachapp.database.DataBaseHelper;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

public class HowToGoFragment extends Fragment {
	private AutoCompleteTextView fromEdit;
	private AutoCompleteTextView toEdit;
	private Button btn;
	private AutoCompleteAdapter departureAdapter;
	private AutoCompleteAdapter arrivalAdapter;
	public static ArrayList<Station> arrivalList;
	public static DataBaseHelper db;
	public Station selectedDeparture;
	public Station selectedArrival;
	
	 @Override 
	public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        setUserVisibleHint(true);
    }
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View v = (LinearLayout)inflater.inflate(R.layout.activity_how_to_go_fragment, container, false);
		fromEdit = (AutoCompleteTextView) v.findViewById(R.id.editText1);
		
		db=new DataBaseHelper(this.getActivity().getApplicationContext());	
		ArrayList<Station> departureList=db.getStationList("");
		
		departureAdapter =new AutoCompleteAdapter(this.getActivity(),R.layout.auto_complete ,R.id.autoCompleteStationName, departureList);
		fromEdit.setAdapter(departureAdapter);
		departureAdapter.notifyDataSetChanged();
		
		fromEdit.setOnItemClickListener(departureClickListener);
		//fromEdit.setThreshold(1);
		
		
		toEdit =(AutoCompleteTextView) v.findViewById(R.id.editText2);
		//ArrayList<Station> arrivalList=db.getStationList("");
		
		arrivalAdapter =new AutoCompleteAdapter(this.getActivity(),R.layout.auto_complete , R.id.autoCompleteStationName, departureList);
	    toEdit.setAdapter(arrivalAdapter);
	    arrivalAdapter.notifyDataSetChanged();
	    toEdit.setOnItemClickListener(arrivalClickListener);
	    
		btn = (Button) v.findViewById(R.id.button1);
		btn.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if(!selectedDeparture.getStationCode().substring(0,5).equals(selectedArrival.getStationCode().substring(0,5))){

					Log.i("startposition", selectedDeparture.getStationCode());
					Log.i("endposition", selectedArrival.getStationCode() );
					Intent intent = new Intent();
	                intent.setClass(getActivity(), ListActivity.class);
	                intent.putExtra("start",selectedDeparture.getStationCode());
	                intent.putExtra("end", selectedArrival.getStationCode());
	                intent.putExtra("loc1",fromEdit.getText().toString());
	                intent.putExtra("loc2", toEdit.getText().toString());
					startActivity(intent);	
				}
				else
					Toast.makeText(getActivity(), "Please enter different locations!", Toast.LENGTH_LONG).show();
			}
			
		});
		if (container == null) {
            return null;
        }
		return v;
	}
	private OnItemClickListener departureClickListener = new 
	         OnItemClickListener() {	           
				@Override
				public void onItemClick(AdapterView<?> arg0, View arg1,
						int arg2, long arg3) {
					// TODO Auto-generated method stub
					selectedDeparture=(Station)arg0.getItemAtPosition(arg2);
					Log.i("select: ", selectedDeparture.getStationName()+ " - "+ selectedDeparture.getStationCode());
				}
				
	};
	private OnItemClickListener arrivalClickListener = new 
	         OnItemClickListener() {	           
				@Override
				public void onItemClick(AdapterView<?> arg0, View arg1,
						int arg2, long arg3) {
					// TODO Auto-generated method stub
					selectedArrival=(Station)arg0.getItemAtPosition(arg2);
					Log.i("select: ",selectedArrival.getStationName()+ " - " +selectedArrival.getStationCode());
				}
				
	};
	
}
